﻿using System.Reflection;
using System.Reflection.Metadata;
using System.Runtime.Loader;

namespace ConsoleApp_Test
{
    internal class Program
    {
        private static readonly Dictionary<string, AssemblyLoadContext> _contexts = new();
        private static readonly Dictionary<string, Assembly> _assemblies = new();

        public static void LoadPlugin(string dllPath)
        {
            if (!File.Exists(dllPath))
                return;

            var contextName = Path.GetFileNameWithoutExtension(dllPath);
            var context = new AssemblyLoadContext(contextName, true);

            using var stream = new FileStream(dllPath, FileMode.Open, FileAccess.Read);
            var assembly = context.LoadFromStream(stream);

            _contexts.Add(contextName, context);
            _assemblies.Add(contextName, assembly);

            var types = assembly.GetExportedTypes();

            foreach (var type in types)
            {
                Console.WriteLine($"{type.FullName}");

                var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance);
                foreach (var method in methods) 
                {
                    Console.WriteLine($"|----{method.Name}");

                    var attributes = method.GetCustomAttributes(true);

                    foreach (var attribute in attributes)
                    {
                        Console.WriteLine($"|----*----{attribute.GetType().Name}");
                        
                    }

                }
            }
        }




        static void Main(string[] args)
        {
            string dllPath = @"H:\temp\Reflection_Test\Reflection_Test\Test_ClassLibrary\bin\Debug\net8.0\Test_ClassLibrary.dll";
            LoadPlugin(dllPath);

        }

    }
}

